define(['../../../../js/lib/other/sweet-alert'],function() {
    return ['$scope','$location', '$interval','$timeout','bizProxy', function($scope,$location,$interval,$timeout,bizProxy) {
    	$scope.$emit("top-show",'true');//顶部显示
    	$scope.$emit('menuTypeLight','loan'); 	//控制底部menu高亮
    	$scope.$emit('bub-menu','true'); 	//隐藏底部menu
    	$scope.$emit('flag-tag','3-2'); 	//显示底部文字

    	var imageProdUrl = "";
    	$scope.isSlide_up = false;
		//点击上传图片
		$scope.upLoadImage = function(){
			$scope.isSlide_up = true;
		}
		
		//点击打开相机
		$scope.openCamera = function(){
			$scope.isSlide_up = false;
			invokePlugin(Camera.PictureSourceType.CAMERA);
		}
		
		//点击打开相册
		$scope.openPhoto = function(){
			$scope.isSlide_up = false;
			invokePlugin(Camera.PictureSourceType.PHOTOLIBRARY);
		}
		
		//点击取消
		$scope.cancle = function(){
			$scope.isSlide_up = false;
		}
		
		$scope.submitApply = function(){
			if(imageProdUrl == ""){
				$scope.$emit("promptAlertMsg","请先上传图片");
        		$scope.$emit("showCollect",true);
				$timeout(function(){
					$scope.$emit("showCollect",false);
				},transData.promptTime);
				return;
			}
			submitApplyFor();
		}
		
		//调用插件
		 function invokePlugin(typeObj){
			 var option = {};
//				本地：PHOTOLIBRARY  相机：CAMERA
				option.sourceType = typeObj;
				　//Camera.getPicture(type)->根据选择的“选取图片”的方式进行选取
	            navigator.camera.getPicture(
	　　　　　　　　　 //返回一个imageURI，记录了照片的路径
	                function (imageURI) {
	                	imageProdUrl = imageURI;
//	                	document.querySelector('#notice_upload .main .img_show').style.backgroundImage = 'url('+imageURI+')';
	                	$scope.$apply(function(){
	                		$scope.isShow = true;
	                	})
	                	$timeout(function(){
	                		document.getElementById("loadImg").src = imageURI;
	                	},200)
	                },
	                function (err) {
	                },option);
		 }
		 
		 //上传图片
		 function upLoadPic(applyId){
//				$scope.$emit("loadingChange",true);
				//新建文件上传选项，并设置文件key，name，type
				var options = new FileUploadOptions();
				options.fileKey="file";
				options.fileName=imageProdUrl.substr(imageProdUrl.lastIndexOf('/')+1);
				options.mimeType="image/jpeg";
				//用params保存其他参数，例如昵称，年龄之类
				var params = {};
				params['name'] = "图片名称";
				params['loanApplyId'] = applyId;
				//把params添加到options的params中
				options.params = params;
				//新建FileTransfer对象
				var ft = new FileTransfer();
				//上传文件
				ft.upload(
					imageProdUrl,
				    encodeURI('http://wx.winhc.cn/uploadImage.ajax'),//把图片及其他参数发送到这个URL，相当于一个请求，在后台接收图片及其他参数然后处理
				    uploadSuccess,
				    uploadError,
				    options);
				//upload成功的话
				function uploadSuccess(r) {
				    if(r.response == "00"){
				　　　　 //返回前一页面
//				        $navHistory.back();
				    	$timeout(function(){
			    			$scope.$emit("loadingChange",false);
			    			$scope.$emit("goNext","/sucLoanApply");
				    	//	$scope.$emit("goNext","/loanSurvey/"+applyId);
				    	},0);
				    }else{
				    	$timeout(function(){
					    	$scope.$emit("loadingChange",false);
				    	},0);
				    }
				} 
				//upload失败的话
				function uploadError(error) {
					$scope.$emit("loadingChange",false);
					$scope.$emit("promptAlertMsg","图片上传失败");
	        		$scope.$emit("showCollect",true);
					$timeout(function(){
						$scope.$emit("showCollect",false);
					},transData.promptTime);
				}
		 }
		 
		//提交申请到代理
    	function submitApplyFor(){
    		var headerInfo = {},bizInfo = {};
        	headerInfo.pageNum = '';
        	headerInfo.pageSize = '';
        	headerInfo.sessionId = transData.sessionId;
        	
        	bizInfo = bodyInfo.loanFormData;
        	
    		$scope.$emit("loadingChange",true);
    		bizProxy.queryProxyBizData("loanApply",headerInfo,bizInfo,
    				function(tempData){//SUCCESS
						var loanApplyIdTemp = JSON.parse(tempData.body).applyId;
						$timeout(function(){
							upLoadPic(loanApplyIdTemp);
						},0);
		    		},function(data){
		    			if(data != ""){//FAIL
		    				$scope.$emit("loadingChange",false);
							if(data.errorCode == ERROR_CODE.noLogin){
								FIREFLY("", data.errorMsg,"",function(){
										//调用退出登录
										transData.mobileNo = "";
		    							transData.password = "";
		    							transData.sessionId = "";
		    							transData.userType = "";
		    							resultData.loginUserData = "";
		    							resultData.loginResultData = "";
		    							$timeout(function(){
		    								$scope.$emit("goNext","/mine");
		    							},0);
								});
							}else{
								$scope.$emit("promptAlertMsg",data.errorMsg);
								$scope.$emit("showCollect",true);
								$timeout(function(){
									$scope.$emit("showCollect",false);
								},transData.promptTime);
							}
		    			}else{
		    				$scope.$emit("loadingChange",false);
			    			$scope.$emit("promptAlertMsg","连接超时");
							$scope.$emit("showCollect",true);
							$timeout(function(){
								$scope.$emit("showCollect",false);
							},transData.promptTime);
		    			}
		    		});
    	}
        $scope.$apply();
    }];
});